/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package
{
	import mx.collections.ICollectionView;
	import mx.controls.CheckBox;
	import mx.controls.DataGrid;
	import mx.core.mx_internal;

	use namespace mx_internal;

	public class Check extends CheckBox
	{

		public function Check()
		{
		}

		override public function set data(value:Object):void
		{
			value = value as TestTarget;
			super.data = value;

			if (data !== null)
			{
				selected = (data as TestTarget).enabled;
			}
		}

		mx_internal override function setSelected(value:Boolean, isProgrammatic:Boolean = false):void
		{
			super.setSelected(value, isProgrammatic);

			var target:TestTarget = data as TestTarget;
			if (target)
			{
				if (target.enabled !== value)
				{
					target.enabled = value;
					var grid:DataGrid = owner as DataGrid;
					if (grid !== null)
					{
						var v:int = grid.verticalScrollPosition;
						var collection:ICollectionView = grid.dataProvider as ICollectionView;
						if (collection !== null)
						{
							collection.refresh();
						}

						var data:TestTargetData = collection as TestTargetData;
						if (data !== null)
						{
							data.save();
						}
						
						grid.validateNow();
						grid.verticalScrollPosition = v;
					}
				}
			}
		}
	}
}
